var express = require('express');
var router = express.Router();
//获取数据库api中get方法
const {get,add,getOne,updata,delImg,searchMove,remove,findOne, delPoster,changeMovies,moviesListAll} =require("../api/movies")

// 获取所有的电影  排片用的
router.get('/moviesListAll', async function (req, res, next) {
    const data = await moviesListAll();
    res.send(data);
  });

router.get('/',async function(req, res, next) {
    const {curPage,eachPage}=req.query;
    const data=await get({curPage,eachPage});
    res.send(data)
});
router.get('/:_id',async function(req, res, next) {
    const{_id}=req.params;
    const data=await getOne(_id);
     res.send(data)
 });
//  查询电影
 router.post('/findmovie', async function (req, res, next) {
     
    const { movie } = req.body;
    const data = await searchMove(movie);
    res.send(data);
});
router.post('/add',async function(req, res, next) {
    // console.log(1);
    const {_id,cname,ename,type,time,upDate,score,area, count,intro,isClassic,director,actor,}=req.body;
    const data=await add({_id,cname,ename,type,time,area,upDate,score, count,intro,isClassic,director,actor,});
    res.send(data)

});
router.patch('/:_id',async function(req, res, next) {
   const{_id}= req.params;
   const data=req.body;
   await updata({_id},data);
   const result=await getOne(_id);
    res.send(result)
});
// 删除电影
router.post('/remove',async function(req, res, next) {
    const{_id}= req.body;
    await remove({_id});
    res.send({status:true})
});
// 通过id查找
router.get('/findOne/:_id', async function (req, res, next) {
    // params去做路径解析
    const { _id } = req.params;
    const data = await findOne(_id);
    res.send(data)
});
// 删除照片
router.delete('/:_id/imgs/:url', async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    await delImg({ _id, url });
    //获取到修改保存后的最新数据并返回
    const data = await findOne({ _id });
    res.send(data);

});
router.delete('/:_id/poster/:url', async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    await delPoster({ _id, url });
    //获取到修改保存后的最新数据并返回
    const data = await findOne({ _id });
    res.send(data);

});

// 修改
router.put("/change", async function (req, res, next) {
    const newData = req.body;
    await changeMovies(newData);
    res.send({ status: true });
})
module.exports=router;
